package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.service.impl.OrderCoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Author: MH
 * @Date: 2022-08-25 10:25
 * @Description: 订单模块中创建订单和删除订单
 */
@RestController
@RequestMapping("shopping")
public class MhShoppingOrderController {

    @Autowired
    OrderCoreServiceImpl orderCoreService;

    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest request) {
        String userInfo = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        long uid = Long.parseLong(jsonObject.get("uid").toString());
        createOrderRequest.setUserId(uid);
        createOrderRequest.setUniqueKey(UUID.randomUUID().toString());
        String order = orderCoreService.createOrder(createOrderRequest);
        return new ResponseUtil().setData(order);
    }

    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable String id) {
        orderCoreService.deleteOrder(id);
        return new ResponseUtil().setData("成功");
    }
}
